package com.wy.Service.impl;

import com.wy.Service.HuzuService;
import com.wy.Service.TousuService;
import com.wy.dao.HuzuDao;
import com.wy.dao.ResideDao;
import com.wy.dao.TousuDao;
import com.wy.pojo.Reside;
import com.wy.pojo.Tousu;
import com.wy.vo.HuzuDto;
import com.wy.vo.PageVo;
import com.wy.vo.TousuDto;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service("tousuService")
@Transactional(value="transactionManager", rollbackFor = Exception.class)
public class ToususrviceImpl extends BaseServiceImpl implements TousuService {

    @Resource
    private TousuDao tousuDao;

    @Resource
    private ResideDao resideDao;

    @Override
    public PageVo searchAll(Integer current, Integer pageSize) {
        PageRequest pageRequest= PageRequest.of(current-1,pageSize);
        Page page= tousuDao.findAll(pageRequest);
        return new PageVo(page);
    }

    @Override
    public PageVo searchAll(Integer current, Integer pageSize, TousuDto dto) {
        StringBuilder sbd=new StringBuilder("select k from Tousu k where 1=1 ");
        if(!StringUtils.isEmpty(dto.getName())){
            sbd.append(" and k.name like :name");
            dto.setName("%"+dto.getName()+"%");
        }
        if(!StringUtils.isEmpty(dto.getTousur())){
            sbd.append(" and k.tousur like :tousur");
            dto.setTousur("%"+dto.getTousur()+"%");
        }
        if(!StringUtils.isEmpty(dto.getTousur())){
            sbd.append(" and k.toudate like :toudate");
            dto.setToudate("%"+dto.getToudate()+"%");
        }
        if(!StringUtils.isEmpty(dto.getTousur())){
            sbd.append(" and k.sldata like :sldata");
            dto.setSldata("%"+dto.getSldata()+"%");
        }
        return super.searchPagination(current,pageSize,sbd.toString(),dto);
    }

    @Override
    public List<Reside> searchReside() {
        return resideDao.findAll();
    }

    @Override
    public void del(Integer id) {
        tousuDao.deleteById(id);
    }

    @Override
    public void add(Tousu tousu) {
        tousuDao.save(tousu);
    }

    @Override
    public void update(Integer id,String sljeg,String sf,String pingj) {
        tousuDao.updateRecord(id,sljeg,sf,pingj);
    }

}
